/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderIndicator;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.provider.PageVideoSliderAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 九宫格视频滑动
 *
 * @since 2021-04-08
 */
public class BannerNineGridVideoPagerSlice extends AbilitySlice implements PageVideoSliderAdapter.OnItemClickListener {
    ArrayList<String> pages = new ArrayList<>();
    List<ImageViewInfo> videoImageList = new ArrayList<>();
    List<ImageViewInfo> videoImageList1 = new ArrayList<>();
    List<ImageViewInfo> videoImageList2 = new ArrayList<>();
    List<ImageViewInfo> videoImageList3 = new ArrayList<>();
    PageSlider pageSlider, pageVideoSlider;
    PageSliderIndicator pageIndicator;
    private String mPosition, index;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_banner_nine_grid_pager);
        if (intent != null) {
            mPosition = intent.getStringParam("position");
            index = intent.getStringParam("index");
        }
        initView();
        initData();
    }

    private void initView() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageIndicator = (PageSliderIndicator) findComponentById(ResourceTable.Id_page_indicator);
        pageVideoSlider = (PageSlider) findComponentById(ResourceTable.Id_page_video_slider);
        pageSlider.setVisibility(Component.HIDE);
        pageVideoSlider.setVisibility(Component.VISIBLE);
        ShapeElement normalElement = new ShapeElement();
        normalElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#FF7E7E7E")));
        normalElement.setAlpha(168);
        normalElement.setShape(ShapeElement.OVAL);
        normalElement.setBounds(0, 0, 25, 25);
        ShapeElement selectedElement = new ShapeElement();
        selectedElement.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));
        selectedElement.setShape(ShapeElement.OVAL);
        selectedElement.setBounds(0, 0, 25, 25);
        pageIndicator.setItemElement(normalElement, selectedElement);
        pageIndicator.setItemOffset(20);
        pageIndicator.setPageSlider(pageVideoSlider);
        pageIndicator.addOnSelectionChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                pageIndicator.setSelected(i);
            }
        });

    }

    private void initData() {
        if ("0".equals(mPosition)) {
            videoImageList.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-05-25/b146e104069c2bd0590bb919269193c4/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            for (int i = 0; i < videoImageList.size(); i++) {
                pages.add(videoImageList.get(i).getUrl());
            }
            PageVideoSliderAdapter adapter = new PageVideoSliderAdapter(pages, this, this, this);
            pageVideoSlider.setProvider(adapter);
            pageIndicator.setSelected(Integer.parseInt(index));
        } else if ("1".equals(mPosition)) {
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-05-25/b146e104069c2bd0590bb919269193c4/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed.mp4",
                "https://pic.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList1.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            for (int i = 0; i < videoImageList1.size(); i++) {
                pages.add(videoImageList1.get(i).getUrl());
            }
            PageVideoSliderAdapter adapter1 = new PageVideoSliderAdapter(pages, this, this, this);
            pageVideoSlider.setProvider(adapter1);
            pageIndicator.setSelected(Integer.parseInt(index));
        } else if ("2".equals(mPosition)) {
            videoImageList2.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList2.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-05-25/b146e104069c2bd0590bb919269193c4/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList2.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList2.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            for (int i = 0; i < videoImageList2.size(); i++) {
                pages.add(videoImageList2.get(i).getUrl());
            }
            PageVideoSliderAdapter adapter2 = new PageVideoSliderAdapter(pages, this, this, this);
            pageVideoSlider.setProvider(adapter2);
            pageIndicator.setSelected(Integer.parseInt(index));
        } else {
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2018-06-07/cf673556cce54ab9cf4633fd7d9d0d46.mp4",
                "https://pic.vjshi.com/2018-06-06/caa296729c8e6e41e6aff2aadf4feff3/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://img44.photophoto.cn/20170730/0018090594006661_s.jpg"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
                "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424.mp4",
                "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            videoImageList3.add(new ImageViewInfo(
                "https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
                "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                    + "x-oss-process=style/resize_w_285_crop_h_428"));
            for (int i = 0; i < videoImageList3.size(); i++) {
                pages.add(videoImageList3.get(i).getUrl());
            }
            PageVideoSliderAdapter adapter3 = new PageVideoSliderAdapter(pages, this, this, this);
            pageVideoSlider.setProvider(adapter3);
            pageIndicator.setSelected(Integer.parseInt(index));
        }
    }

    @Override
    public void onItemClick(Component component, int position) {
        if ("0".equals(mPosition)) {
            String mUrl = videoImageList.get(position).getVideoUrl();
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.VideoPlayAbility")
                .build();
            intent.setParam("url", mUrl);
            intent.setOperation(operation);
            startAbility(intent);
        } else if ("1".equals(mPosition)) {
            String mVideoUrl = videoImageList1.get(position).getVideoUrl();
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.VideoPlayAbility")
                .build();
            intent.setParam("url", mVideoUrl);
            intent.setOperation(operation);
            startAbility(intent);
        } else if ("2".equals(mPosition)) {
            String mVideoUrl = videoImageList2.get(position).getVideoUrl();
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.VideoPlayAbility")
                .build();
            intent.setParam("url", mVideoUrl);
            intent.setOperation(operation);
            startAbility(intent);
        } else {
            String mVideoUrl = videoImageList3.get(position).getVideoUrl();
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.VideoPlayAbility")
                .build();
            intent.setParam("url", mVideoUrl);
            intent.setOperation(operation);
            startAbility(intent);
        }
    }
}
